<?php

namespace App\Controller\Api\Consultant;

use App\Controller\Api\BaseController;
use App\Model\ConfusionCategory;
use App\Model\ConsultantShortcut;
use App\Model\Language;
use App\Model\User;
use App\Model\UserDatum;
use App\Model\UserShortcut;
use App\Service\TalkService;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\RequestMapping;
use Psr\Http\Message\ResponseInterface;

/**
 * @Controller(prefix="/consultant/v1/talk")
 */
class TalkController extends BaseController
{

    /**
     * @Inject()
     * @var TalkService
     */
    public $talkService;


    /**
     * @RequestMapping(path="getToken")
     * @return ResponseInterface
     */
    public function getToken()
    {


        $tokenInfo = $this->talkService->getEasemobToken($this->uid());
        if (is_array($tokenInfo)) {

            //延迟处理用户状态 暂时处理20秒之后更新用户状态
//            $this->jobService->SyncUserOnlineStatus($this->uid(),20);

            return $this->response->returnSuccess($tokenInfo);
        } else {
            return $this->response->returnFail();
        }

    }


    /**
     * 用户话术
     * @RequestMapping(path="userShortcutList")
     * @return ResponseInterface
     */
    public function userShortcutList()
    {

        // todo ---缓存
        $lang = getLang();
        $list = ConsultantShortcut::where(['lang' => $lang])->orderByDesc('sort')->get()->toArray();
        return $this->response->returnSuccess(['list' => $list]);

    }


    /**
     * 用户聊天小卡片
     * @RequestMapping(path="userChatInfo")
     * @return ResponseInterface
     */
    public function userChatInfo()
    {
        $params = $this->request->query();


        $this->validate($params,[
            'uid'=>'required'
        ]);
        $id = $params['uid'];
        $userInfo = UserDatum::where(['uid' => $id])
            ->select(['uid','nickname', 'head', 'gender', 'confusion_id as confusionId', 'language'])->first();

        $userInfo = $userInfo->toArray();

        if ($userInfo['confusionId']) {
            $userInfo['confusion'] = ConfusionCategory::whereIn('id', explode(',', $userInfo['confusionId']))->pluck('name');
        } else {
            $userInfo['confusion'] = null;
        }

        if ($userInfo['language']) {

            $userInfo['language'] = Language::where(['id'=>$userInfo['language']])->value('name');
        }

        $userInfo['orderNum'] = 0;


        return $this->response->returnSuccess(['info' => $userInfo]);
    }


}